1 // Copyright 2006, 2007, 2008, 2011, 2012 The Apache Software Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package org.apache.tapestry5.ioc.util;
16
17 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
18 import org.apache.tapestry5.ioc.internal.util.InheritanceSearch;
19
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Map;
23
24 /**
25 * A key component in implementing the "Gang of Four" Strategy pattern. A StrategyRegistry will match up a given input
26 * type with a registered strategy for that type.
27 *
28 * @param <A> the type of the strategy adapter
29 */
30 public final class StrategyRegistry<A>
31 {
32 private final Class<A> adapterType;
33
34 private final boolean allowNonMatch;
35
36 private final Map<Class, A> registrations = CollectionFactory.newMap();
37
38 private final Map<Class, A> cache = CollectionFactory.newConcurrentMap();
39
40 /**
41 * Used to identify types for which there is no matching adapter; we're using it as if it were a ConcurrentSet.
42 */
43 private final Map<Class, Boolean> unmatched = CollectionFactory.newConcurrentMap();
44
45 private StrategyRegistry(Class<A> adapterType, Map<Class, A> registrations, boolean allowNonMatch)
46 {
47 this.adapterType = adapterType;
48 this.allowNonMatch = allowNonMatch;
49
50 this.registrations.putAll(registrations);
51 }
52
53 /**
54 * Creates a strategy registry for the given adapter type. The registry will be configured to require matches.
55 *
56 * @param adapterType the type of adapter retrieved from the registry
57 * @param registrations map of registrations (the contents of the map are copied)
58 */
59 public static <A> StrategyRegistry<A> newInstance(Class<A> adapterType,
60 Map<Class, A> registrations)
61 {
62 return newInstance(adapterType, registrations, false);
63 }
64
65 /**
66 * Creates a strategy registry for the given adapter type.
67 *
68 * @param adapterType the type of adapter retrieved from the registry
69 * @param registrations map of registrations (the contents of the map are copied)
70 * @param allowNonMatch if true, then the registry supports non-matches when retrieving an adapter
71 */
72 public static <A> StrategyRegistry<A> newInstance(
73 Class<A> adapterType,
74 Map<Class, A> registrations, boolean allowNonMatch)
75 {
76 return new StrategyRegistry<A>(adapterType, registrations, allowNonMatch);
77 }
78
79 public void clearCache()
80 {
81 cache.clear();
82 unmatched.clear();
83 }
84
85 public Class<A> getAdapterType()
86 {
87 return adapterType;
88 }
89
90 /**
91 * Gets an adapter for an object. Searches based on the value's class, unless the value is null, in which case, a
92 * search on class void is used.
93 *
94 * @param value for which an adapter is needed
95 * @return the adapter for the value or null if not found (and allowNonMatch is true)
96 * @throws IllegalArgumentException if no matching adapter may be found and allowNonMatch is false
97 */
98
99 public A getByInstance(Object value)
100 {
101 return get(value == null ? void.class : value.getClass());
102 }
103
104 /**
105 * Searches for an adapter corresponding to the given input type.
106 *
107 * @param type the type to search
108 * @return the adapter for the type or null if not found (and allowNonMatch is true)
109 * @throws IllegalArgumentException if no matching adapter may be found and allowNonMatch is false
110 */
111 public A get(Class type)
112 {
113
114 A result = cache.get(type);
115
116 if (result != null) return result;
117
118 if (unmatched.containsKey(type)) return null;
119
120
121 result = findMatch(type);
122
123 // This may be null in the case that there is no match and we're allowing that to not
124 // be an error. That's why we check via containsKey.
125
126 if (result != null)
127 {
128 cache.put(type, result);
129 } else
130 {
131 unmatched.put(type, true);
132 }
133
134 return result;
135 }
136
137 private A findMatch(Class type)
138 {
139 for (Class t : new InheritanceSearch(type))
140 {
141 A result = registrations.get(t);
142
143 if (result != null) return result;
144 }
145
146 if (allowNonMatch) return null;
147
148 // Report the error. These things really confused the hell out of people in Tap4, so we're
149 // going the extra mile on the exception message.
150
151 List<String> names = CollectionFactory.newList();
152 for (Class t : registrations.keySet())
153 names.add(t.getName());
154
155 throw new UnknownValueException(String.format("No adapter from type %s to type %s is available.", type.getName(), adapterType.getName()), null, null,
156 new AvailableValues("registered types", registrations));
157 }
158
159 /**
160 * Returns the registered types for which adapters are available.
161 */
162 public Collection<Class> getTypes()
163 {
164 return CollectionFactory.newList(registrations.keySet());
165 }
166
167 @Override
168 public String toString()
169 {
170 return String.format("StrategyRegistry[%s]", adapterType.getName());
171 }
172 }